home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 10 code / GWorld Drawing / GWorld Routines / CalculateDeltas.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  2.8 KB  |  83 lines  |  [TEXT/KAHL]

  1. /* Routine to take a 32-bit offscreen and display the luminance delta in an 8-bit
  2. ** offscreen grayscale.
  3. ** Note: Both GWorlds must be allocated when this routine is called.
  4. */
  5.  
  6. #include "DemoRoutines.h"
  7.  
  8. void CalculateDeltas( GWorldPtr src, GWorldPtr dst )
  9. {
  10.     PixMapHandle    srcPixMap, dstPixMap;
  11.     short        srcRowBytes, dstRowBytes;
  12.     long            *srcBaseAddr, *dstBaseAddr, *dstAddr;
  13.     long            *srcAddr1;
  14.     short        mmuMode;
  15.     short        row, column;
  16.     unsigned char    lum1,lum2;
  17.     unsigned long    dstLong;
  18.     short        width, height;
  19.  
  20.     srcPixMap = GetGWorldPixMap ( src );
  21.     dstPixMap = GetGWorldPixMap ( dst );
  22.     
  23.     if ( LockPixels ( srcPixMap ) && LockPixels( dstPixMap) )     /* lock the pixmaps */
  24.     {
  25.         srcBaseAddr = (long *) GetPixBaseAddr ( srcPixMap );    /* get the address of the pixmap */
  26.         srcRowBytes = (**srcPixMap).rowBytes & 0x7fff;            /* get the row increment */
  27.         dstBaseAddr = (long *) GetPixBaseAddr ( dstPixMap );    /* get the address of the pixmap */
  28.         dstRowBytes = (**dstPixMap).rowBytes & 0x7fff;            /* get the row increment */
  29.         width = (**srcPixMap).bounds.right-(**srcPixMap).bounds.left;
  30.         height = (**srcPixMap).bounds.bottom-(**srcPixMap).bounds.top;
  31.  
  32.         mmuMode = true32b;
  33.         SwapMMUMode ( &mmuMode );                        /* set the MMU to 32-bit mode */
  34.         for ( row = 0; row < height; row++ )            /* get each pixel in the pixmap */
  35.         {
  36.             srcAddr1 = srcBaseAddr;
  37.             dstAddr = dstBaseAddr;
  38.             lum1 = CalcLuminance( *srcAddr1++ );        /* calc luminance of src pixel */
  39.             for ( column = 0; column < ((width-1)/4); column++ ) 
  40.             {
  41.                 /* 
  42.                 ** Do a long in the destination (4 pixels)
  43.                 ** This is o.k. since memory blocks are always long word aligned. 
  44.                 ** Thus, we will never write over the right hand edge.
  45.                 */
  46.                 dstLong = 0;
  47.                 lum2 = CalcLuminance( *srcAddr1++ );
  48.                 dstLong = (unsigned char)((0x100 + lum1 - lum2)>>1);
  49.                 dstLong = dstLong << 8;
  50.                 lum1 = CalcLuminance( *srcAddr1++ );
  51.                 dstLong |= (unsigned char)((0x100 + lum2 - lum1)>>1);
  52.                 dstLong = dstLong<<8;
  53.                 lum2 = CalcLuminance( *srcAddr1++ );
  54.                 dstLong |= (unsigned char)((0x100 + lum1 - lum2)>>1);
  55.                 dstLong = dstLong << 8;
  56.                 lum1 = CalcLuminance( *srcAddr1++ );
  57.                 dstLong |= (unsigned char)((0x100 + lum2 - lum1)>>1);
  58.                 *dstAddr++ = dstLong;
  59.             }
  60.  
  61.             srcBaseAddr = (long *) ( (char *) srcBaseAddr + srcRowBytes );    /* go to the next row */
  62.             dstBaseAddr = (long *) ( (char *) dstBaseAddr + dstRowBytes );    /* go to the next row */
  63.         }
  64.         SwapMMUMode ( &mmuMode );                        /* restore the previous MMU mode */
  65.         UnlockPixels ( srcPixMap );                        /* unlock the pixmap */
  66.         UnlockPixels ( dstPixMap );                        /* unlock the pixmap */
  67.     }
  68. }
  69.  
  70. unsigned char CalcLuminance( long RGBval )
  71. {
  72. unsigned char    result;
  73. unsigned char    red, green, blue;
  74.  
  75.  
  76.     blue = (char) (RGBval);
  77.     green = (char) (RGBval>>8);
  78.     red = (char) (RGBval>>16);
  79.  
  80.     result = (blue*2 + green*5 + red*9)/16;    
  81.     return result;
  82.  
  83. }